{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a target=\"_blank\" href=\"https://colab.research.google.com/github/AI4Finance-Foundation/FinRL-Tutorials/blob/master/2-Advance/FinRL_Meta_Integrate_Trends_data_to_DOW_Jones.ipynb\">\n",
    "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
    "</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "mh1G9TJ4n1ZC",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install wrds\n",
    "!pip install swig\n",
    "!pip install -q condacolab\n",
    "import condacolab\n",
    "condacolab.install()\n",
    "!apt-get update -y -qq && apt-get install -y -qq cmake libopenmpi-dev python3-dev zlib1g-dev libgl1-mesa-glx swig\n",
    "!pip install git+https://github.com/AI4Finance-Foundation/FinRL.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "rtOAf118Xd0S",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip3 install optuna"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Bc8-aemf2fGo",
    "outputId": "7822d719-5ad3-427c-cdaa-83e227cf5733",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "# matplotlib.use('Agg')\n",
    "import datetime\n",
    "import os\n",
    "import optuna\n",
    "import torch \n",
    "\n",
    "from finrl import config\n",
    "from finrl.meta.preprocessor.yahoodownloader import YahooDownloader\n",
    "from finrl.meta.preprocessor.preprocessors import FeatureEngineer, data_split\n",
    "#from finrl.meta.env_stock_trading.env_stocktrading import StockTradingEnv\n",
    "from finrl.meta.env_stock_trading.env_stocktrading_np import StockTradingEnv\n",
    "from finrl.agents.stablebaselines3.models import DRLAgent as DRLAgent_sb3\n",
    "\n",
    "from finrl.plot import backtest_stats, backtest_plot, get_daily_return, get_baseline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4oJitZly2KDe"
   },
   "source": [
    "## Custom data processor\n",
    "\n",
    "* Only add a functionality add_user_defined_features to data processor "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bA1mZX0G21xF",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from finrl.meta.data_processors.processor_alpaca import Alpaca as Alpaca\n",
    "from finrl.meta.data_processors.processor_wrds import Wrds as Wrds\n",
    "from finrl.meta.data_processors.processor_yahoofinance import Yahoofinance as YahooFinance\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "class Custom_DataProcessor():\n",
    "    def __init__(self, data_source, **kwargs):\n",
    "        if data_source == 'alpaca':\n",
    "            \n",
    "            try:\n",
    "                API_KEY= kwargs.get('API_KEY')\n",
    "                API_SECRET= kwargs.get('API_SECRET')\n",
    "                API_BASE_URL= kwargs.get('API_BASE_URL')\n",
    "                self.processor = Alpaca(API_KEY, API_SECRET, API_BASE_URL)\n",
    "                print('Alpaca successfully connected')\n",
    "            except:\n",
    "                raise ValueError('Please input correct account info for alpaca!')\n",
    "                \n",
    "        elif data_source == 'wrds':\n",
    "            self.processor = Wrds()\n",
    "            \n",
    "        elif data_source == 'yahoofinance':\n",
    "            self.processor = YahooFinance()\n",
    "        \n",
    "        else:\n",
    "            raise ValueError('Data source input is NOT supported yet.')\n",
    "    \n",
    "    def download_data(self, ticker_list, start_date, end_date, \n",
    "                      time_interval):\n",
    "        self.processor.download_data(ticker_list = ticker_list,\n",
    "                                          start_date = start_date, \n",
    "                                          end_date = end_date,\n",
    "                                          time_interval = time_interval)\n",
    "        self.dataframe = self.processor.dataframe\n",
    "    \n",
    "    def clean_data(self):\n",
    "        self.processor.clean_data()\n",
    "        self.dataframe = self.processor.dataframe\n",
    "    \n",
    "    def add_technical_indicator(self, tech_indicator_list):\n",
    "        # self.tech_indicator_list = tech_indicator_list\n",
    "        self.processor.add_technical_indicator(tech_indicator_list)\n",
    "        self.dataframe = self.processor.dataframe\n",
    "    \n",
    "    def add_turbulence(self):\n",
    "        self.processor.add_turbulence(df)\n",
    "        self.dataframe = self.processor.dataframe\n",
    "    \n",
    "    def add_vix(self):\n",
    "        self.processor.add_vix()\n",
    "        self.dataframe = self.processor.dataframe\n",
    "    \n",
    "    def add_user_defined_features(self,user_df):\n",
    "        df = self.processor.dataframe.copy()\n",
    "        df = df.merge(user_df, how='left', left_on=[\n",
    "            'time', 'tic'], right_on=['time', 'tic'])\n",
    "        self.processor.dataframe = df\n",
    "        self.dataframe = df\n",
    "    \n",
    "    def df_to_array(self, tech_indicator_list, if_vix) -> np.array:\n",
    "        price_array,tech_array,turbulence_array = self.processor.df_to_array(\n",
    "                                                tech_indicator_list,\n",
    "                                                if_vix)\n",
    "        #fill nan with 0 for technical indicators\n",
    "        tech_nan_positions = np.isnan(tech_array)\n",
    "        tech_array[tech_nan_positions] = 0\n",
    "        \n",
    "        return price_array,tech_array,turbulence_array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7d1DwraO2NLm"
   },
   "source": [
    "## User defined column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "zywLIWou9fkj",
    "outputId": "b68b7a92-c66c-41f4-dd2f-c3bbe2539031",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "# !gdown --id \"1sp11dtAJGGqC-3UdSn774ZD1zWCsqbn4\"\n",
    "!gdown --id \"1m63ncE-BYlS77u5ejYTte9Nmh35DWhzp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "wbA9X_XI-uK4",
    "outputId": "042b9bab-17ef-466b-fb16-b8c422844713",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "!unzip \"/content/Pytrends.zip\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bb72z3LV-9ov",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "ticker_list = config.DOW_30_TICKER\n",
    "#Pytrends dataframe\n",
    "def get_user_df():\n",
    "    pytrends_list = os.listdir('Pytrends_Data')\n",
    "    \n",
    "    user_df = pd.DataFrame()\n",
    "    for pytrend in pytrends_list:\n",
    "        tic_name = pytrend.split('_')[0]\n",
    "        if tic_name in ticker_list:\n",
    "            file_name = os.path.join('Pytrends_Data', pytrend)\n",
    "            temp_user_df = pd.read_csv(file_name)\n",
    "            temp_user_df.rename(columns={temp_user_df.columns[1]:'trends'},inplace=True)\n",
    "            temp_user_df.rename(columns={temp_user_df.columns[0]:'time'},inplace=True)\n",
    "            temp_user_df['tic'] = tic_name\n",
    "            user_df = user_df.append(temp_user_df, ignore_index=True)\n",
    "    return user_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "CAIAVqhO_KL-",
    "outputId": "304760c2-c020-4dea-f1f0-0f80c59677e5",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "user_df = get_user_df()\n",
    "len(user_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "gXOeP6boA0X5"
   },
   "source": [
    "## Training and testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "XndhDRznAz1J",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "technical_indicator_list = []\n",
    "\n",
    "info_col = technical_indicator_list + ['trends']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "IMnMcvvzgC3Q",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "def ppo_sample_parameters(trial:optuna.Trial):\n",
    "  batch_size = trial.suggest_categorical(\"batch_size\", [64, 128, 256, 512])\n",
    "  n_steps = trial.suggest_categorical(\"n_steps\", [128, 256, 512, 1024, 2048])\n",
    "  learning_rate = trial.suggest_loguniform(\"learning_rate\", 1e-5, 1)\n",
    "  ent_coef = trial.suggest_loguniform(\"ent_coef\", 0.00000001, 0.1)\n",
    "\n",
    "  return {\n",
    "      \"batch_size\": batch_size,\n",
    "      'n_steps': n_steps,\n",
    "      'learning_rate': learning_rate,\n",
    "      'ent_coef': ent_coef \n",
    "  }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "8n9oe7X0kkh0",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "os.makedirs('tuned_models',exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "089dz_19AheC",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "def get_train_env(start_date, end_date, ticker_list, data_source, time_interval,model_name,env,\n",
    "          info_col, if_vix=True,\n",
    "          **kwargs):\n",
    "\n",
    "    DP = Custom_DataProcessor(data_source, **kwargs)\n",
    "    DP.download_data(ticker_list, start_date, end_date, time_interval)\n",
    "    DP.clean_data()\n",
    "    DP.add_user_defined_features(user_df) #Adding Google trends data to our state space\n",
    "    DP.add_technical_indicator(technical_indicator_list)\n",
    "    if if_vix:\n",
    "        DP.add_vix()\n",
    "    # Passed info col instead of tech_indicator_list.\n",
    "    price_array, tech_array, turbulence_array = DP.df_to_array(\n",
    "        data,info_col, if_vix)\n",
    "    env_config = {'price_array':price_array,\n",
    "              'tech_array':tech_array,\n",
    "              'turbulence_array':turbulence_array,\n",
    "              'if_train':True}\n",
    "    env_instance = env(config=env_config)\n",
    "    \n",
    "\n",
    "    return env_instance\n",
    "\n",
    "def objective(trial:optuna.Trial):\n",
    "    agent_params = ppo_sample_parameters(trial)\n",
    "    tune_cwd = 'tuned_models/'+str(model_name)+'_' + str(agent_params.values())\n",
    "    agent = DRLAgent_sb3(env = train_env_instance)\n",
    "\n",
    "    model = agent.get_model(model_name, model_kwargs = agent_params)\n",
    "    trained_model = agent.train_model(model=model, \n",
    "                            tb_log_name=model_name,\n",
    "                            total_timesteps=total_timesteps)\n",
    "    clear_output(wait=True)\n",
    "    trained_model.save(tune_cwd)\n",
    "\n",
    "    val_sharpe,_ = val_or_test(val_env_instance,tune_cwd,model_name)\n",
    "\n",
    "    return val_sharpe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "-bYmjbANiPpi",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "def calculate_sharpe(df):\n",
    "  df['daily_return'] = df['account_value'].pct_change(1)\n",
    "  if df['daily_return'].std() !=0:\n",
    "    sharpe = (252**0.5)*df['daily_return'].mean()/ \\\n",
    "          df['daily_return'].std()\n",
    "    return sharpe\n",
    "  else:\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "WvrBYt3dBQq9",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "def get_test_env(start_date, end_date, ticker_list, data_source, time_interval, \n",
    "         info_col, env, model_name, if_vix = True,\n",
    "         **kwargs):\n",
    "    #fetch data\n",
    "    DP = Custom_DataProcessor(data_source, **kwargs)\n",
    "    DP.download_data(ticker_list, start_date, end_date, time_interval)\n",
    "    DP.clean_data()\n",
    "    DP.add_user_defined_features(user_df)\n",
    "    DP.add_technical_indicator(technical_indicator_list)\n",
    "    \n",
    "    if if_vix:\n",
    "        DP.add_vix()\n",
    "    price_array, tech_array, turbulence_array = DP.df_to_array(info_col, if_vix)\n",
    "    \n",
    "    env_config = {'price_array':price_array,\n",
    "            'tech_array':tech_array,\n",
    "            'turbulence_array':turbulence_array,\n",
    "            'if_train':False}\n",
    "    test_env_instance = env(config=env_config)\n",
    "    return test_env_instance\n",
    "\n",
    "def val_or_test(test_env_instance,cwd,model_name): \n",
    "    episode_total_assets = DRLAgent_sb3.DRL_prediction_load_from_file(\n",
    "                                  model_name=model_name, \n",
    "                                  environment = test_env_instance,\n",
    "                                  cwd = cwd)\n",
    "    sharpe_df = pd.DataFrame(episode_total_assets,columns=['account_value'])\n",
    "\n",
    "    return calculate_sharpe(sharpe_df),sharpe_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "BEYI2a27BzE6",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from finrl.meta.env_stock_trading.env_stocktrading_np import StockTradingEnv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bV_wGrxkCI9c",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "TRAIN_START_DATE = '2012-01-01'\n",
    "TRAIN_END_DATE = '2019-07-30'\n",
    "\n",
    "VAL_START_DATE = '2019-08-01'\n",
    "VAL_END_DATE = '2020-07-30'\n",
    "TEST_START_DATE = '2020-08-01'\n",
    "TEST_END_DATE = '2021-10-01'\n",
    "\n",
    "PPO_PARAMS = {\"n_steps\": 2048,\"ent_coef\": 0.01,\"learning_rate\": 0.00025,\"batch_size\": 128}\n",
    "SAC_PARAMS = {\"batch_size\": 128,\"buffer_size\": 100000,\"learning_rate\": 0.0001,\"learning_starts\": 100,\"ent_coef\": \"auto_0.1\",}\n",
    "TD3_PARAMS = {\"batch_size\": 100, \"buffer_size\": 1000000, \"learning_rate\": 0.001}\n",
    "DDPG_PARAMS = {\"batch_size\": 128, \"buffer_size\": 50000, \"learning_rate\": 0.001}\n",
    "A2C_PARAMS = {\"n_steps\": 5, \"ent_coef\": 0.01, \"learning_rate\": 3e-5}\n",
    "\n",
    "\n",
    "data_source = 'yahoofinance'\n",
    "time_interval = '1D'\n",
    "model_name = 'ppo'\n",
    "total_timesteps = 30000\n",
    "\n",
    "env = StockTradingEnv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "7VC22EoxiuoA",
    "outputId": "0cbda68c-04ad-4999-ca6c-8efe2d85cbca",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "train_env_instance = get_train_env(TRAIN_START_DATE, TRAIN_END_DATE, \n",
    "                                   ticker_list, data_source, \n",
    "                                   time_interval,model_name,\n",
    "                                   env,info_col)\n",
    "val_env_instance = get_test_env(VAL_START_DATE, VAL_END_DATE, \n",
    "                                ticker_list, data_source,\n",
    "                                time_interval, info_col, env, model_name)                         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5fE0C5i2kM2r",
    "outputId": "2d1c8c35-06c0-40c1-a07f-780ca9677fd6",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "sampler = optuna.samplers.TPESampler(seed=42)\n",
    "study = optuna.create_study(study_name=\"ppo_study\",direction='maximize',\n",
    "                            sampler = sampler, pruner=optuna.pruners.HyperbandPruner())\n",
    "study.optimize(objective, n_trials=30,catch=(ValueError,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "gKAAoA18poYk",
    "outputId": "36bc940b-c889-4507-865a-fb386784feb0",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "print(study.best_params)\n",
    "print(study.best_value)\n",
    "print(study.best_trial)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "3fb0IPB_neDK",
    "outputId": "59632510-b623-4175-fbb8-4d6b4ab64a59",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "test_env_instance = get_test_env(TEST_START_DATE, TEST_END_DATE, \n",
    "                                ticker_list, data_source,\n",
    "                                time_interval, info_col, env, model_name)    \n",
    "test_cwd = 'tuned_models/'+str(model_name)+'_' + str(study.best_params.values())\n",
    "test_sharpe,df_account_value = val_or_test(test_env_instance,test_cwd,model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mx_MWUwFIp3Q"
   },
   "source": [
    "## Backtesting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xr6xR1zLGvyB",
    "outputId": "4cdb2954-e332-4ef4-8dc1-1741933adc51",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "Custom_DataProcessor('yahoofinance').download_data(ticker_list = [\"^DJI\"],\n",
    "                                                            start_date = TEST_START_DATE, \n",
    "                                                            end_date = TEST_END_DATE, \n",
    "                                                            time_interval = \"1D\")\n",
    "stats = backtest_stats(Custom_DataProcessor.dataframe, value_col_name = 'close')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "c6oEyURLHD30",
    "outputId": "888f0252-f454-41f5-95bd-6668c1386127",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "os.chdir('/content/tuned_models')\n",
    "for test_cwd in os.listdir():\n",
    "  test_sharpe,df_account_value = val_or_test(test_env_instance,test_cwd,model_name)\n",
    "  print(test_cwd,test_sharpe)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "NxR3lL6wKRWT",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "# !mkdir 'drive/MyDrive/tuned_models_DOW_JONES'\n",
    "# !mv 'tuned_models_DOW_JONES' 'drive/MyDrive/tuned_models'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "wewbnmpNKtzK",
    "outputId": "8ba24919-94f9-4259-db6f-ed0541fca6d1",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "#Best test sharpe\n",
    "best_test_cwd = \"ppo_dict_values([64, 128, 0.0007114879943759374, 1.7734195965746112e-05])\"\n",
    "# ppo_dict_values([256, 256, 0.00013931273790066692, 3.4582737549732e-08])\n",
    "\n",
    "test_sharpe,df_account_value = val_or_test(test_env_instance,best_test_cwd,model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xJlX8UHZLUlg",
    "outputId": "0c0a6a1b-a16a-4d32-c08d-c654f8eb0df2",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "account_value_sb3 = list(df_account_value['account_value'])\n",
    "account_value_sb3 = pd.DataFrame({'date':Custom_DataProcessor.dataframe.date,'account_value':account_value_sb3[0:len(account_value_sb3)-1]})\n",
    "perf_stats_all = backtest_stats(account_value=account_value_sb3)\n",
    "perf_stats_all = pd.DataFrame(perf_stats_all)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "id": "NvrqgVH3Lfu_",
    "outputId": "ea15b798-200a-4d55-c3a1-b921f1db1f39",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "account_value_sb3.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "ViJ-ea0YHIpL",
    "outputId": "b0dc1131-4ec3-4147-d86f-b5f011ee130e",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "print(\"==============Compare to DJIA===========\")\n",
    "%matplotlib inline\n",
    "# S&P 500: ^GSPC\n",
    "# Dow Jones Index: ^DJI\n",
    "# NASDAQ 100: ^NDX\n",
    "\n",
    "backtest_plot(account_value_sb3, \n",
    "             baseline_ticker = '^DJI', \n",
    "             baseline_start = account_value_sb3.loc[0,'date'],\n",
    "             baseline_end = account_value_sb3.loc[len(account_value_sb3)-1,'date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dNTLypaPBKiS",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "include_colab_link": true,
   "name": "Demo_FinRL_Meta_Integrate_Trends_data_to_DOW_Jones.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "PyCharm (FinRL)",
   "language": "python",
   "name": "pycharm-2c75a13c"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
